﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using Oracle.DataAccess.Client;

namespace Project01_DBSecurity
{
    public partial class Form_Main : RibbonForm
    {
        public Form_Main()
        {
            InitializeComponent();
            InitSkinGallery();
            InitGrid();

        }
        void InitSkinGallery()
        {
            //SkinHelper.InitSkinGallery(rgbiSkins, true);
        }
        void InitGrid()
        {
        }

		private void ribbonControl_hideAllPage()
		{
			panel_QLTN.Hide();
			panel_QLROLE.Hide();
			panel_QLPROFILE.Hide();
		}

		private void ribbonControl_SelectedPageChanged(object sender, EventArgs e)
		{
			ribbonControl_hideAllPage();
			string tab = ribbonControl.SelectedPage.Name.ToString();
			if (tab == "tab_QLTN")
				panel_QLTN.Show();
			else if (tab == "tab_QLROLE")
				panel_QLROLE.Show();
			else if (tab == "tab_QLPROFILE")
				panel_QLPROFILE.Show();
		}

        private void MainForm_Load(object sender, EventArgs e)
        {
			Form_Login form_login = new Form_Login();
			form_login.ShowDialog();
			ribbonControl.SelectedPageChanged += new EventHandler(ribbonControl_SelectedPageChanged);
        }

		private void QLPROFILE_THEM_ItemClick(object sender, ItemClickEventArgs e)
		{
			Form_Profile form_profile = new Form_Profile(1, null);
			form_profile.ShowDialog();
		}

		private void QLPROFILE_TAI_ItemClick(object sender, ItemClickEventArgs e)
		{
			panel_QLPROFILE_list.Items.Clear();
			OracleCommand comm = new OracleCommand();
			comm.Connection = orcl.conn;
			comm.CommandText = "select name from sys.profname$";
			try
			{
				OracleDataReader reader = comm.ExecuteReader();
				while (reader.Read())
				{
					panel_QLPROFILE_list.Items.Add(reader[0].ToString());
				}
				reader.Close();
				reader.Dispose();
				comm.Dispose();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void QLPROFILE_XOA_ItemClick(object sender, ItemClickEventArgs e)
		{
			if (panel_QLPROFILE_list.SelectedItems.Count > 0)
			{
				string profname = panel_QLPROFILE_list.SelectedItems[0].Text;
				if (MessageBox.Show("Bạn chắc chắn muốn xóa profile \"" + profname + "\" ?", "Warn", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
				{
					OracleCommand comm = new OracleCommand();
					comm.Connection = orcl.conn;
					comm.CommandText = "drop profile \"" + profname + "\"";
					try
					{
						comm.ExecuteNonQuery();
						comm.Dispose();
						MessageBox.Show("Xóa profile \"" + profname + "\" thành công", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);
						panel_QLPROFILE_list.Items.Remove(panel_QLPROFILE_list.SelectedItems[0]);
					}
					catch (Exception ex)
					{
						MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
					}
				}
			}
		}

		private void QLPROFILE_SUA_ItemClick(object sender, ItemClickEventArgs e)
		{
			if (panel_QLPROFILE_list.SelectedItems.Count > 0)
			{
				string prof_name = panel_QLPROFILE_list.SelectedItems[0].Text;
				Form_Profile form_profile = new Form_Profile(2, prof_name);
				form_profile.ShowDialog();
			}
		}

		private void QLPROFILE_XEMUSERS_ItemClick(object sender, ItemClickEventArgs e)
		{
			panel_QLPROFILE_users.Items.Clear();
			if (panel_QLPROFILE_list.SelectedItems.Count > 0)
			{
				string profname = panel_QLPROFILE_list.SelectedItems[0].Text;
				OracleCommand comm = new OracleCommand();
				comm.Connection = orcl.conn;
				comm.CommandText = "select username from sys.dba_users where profile = '" + profname + "'";
				try
				{
					OracleDataReader reader = comm.ExecuteReader();
					while (reader.Read())
					{
						panel_QLPROFILE_users.Items.Add(reader[0].ToString());
					}
					reader.Close();
					reader.Dispose();
					comm.Dispose();
				}
				catch (Exception ex)
				{
					MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}

		private void QLPROFILE_CHITIET_ItemClick(object sender, ItemClickEventArgs e)
		{
			if (panel_QLPROFILE_list.SelectedItems.Count > 0)
			{
				string prof_name = panel_QLPROFILE_list.SelectedItems[0].Text;
				Form_Profile form_profile = new Form_Profile(3, prof_name);
				form_profile.ShowDialog();
			}
		}

		private void QLPROFILE_XEMLIMITS_ItemClick(object sender, ItemClickEventArgs e)
		{
			Form_Profile form_profile = new Form_Profile(4, null);
			form_profile.ShowDialog();
		}

		private void QLROLE_TAI_ItemClick(object sender, ItemClickEventArgs e)
		{
			panel_QLROLE_list.Items.Clear();
			panel_QLROLE_list_curr_user.Items.Clear();
			OracleCommand comm = new OracleCommand();
			comm.Connection = orcl.conn;

			comm.CommandText = "select * from USER_ROLE_PRIVS";
			try
			{
				OracleDataReader reader = comm.ExecuteReader();
				while (reader.Read())
				{
					ListViewItem lvi = panel_QLROLE_list_curr_user.Items.Add(reader["GRANTED_ROLE"].ToString());
					lvi.SubItems.Add(reader["ADMIN_OPTION"].ToString());
				}
				reader.Close();
				reader.Dispose();
				comm.Dispose();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}

			comm.CommandText = "select * from dba_roles";
			try
			{
				OracleDataReader reader = comm.ExecuteReader();
				while (reader.Read())
				{
					ListViewItem lvi = panel_QLROLE_list.Items.Add(reader["role"].ToString());
					lvi.SubItems.Add(reader["password_required"].ToString());
				}
				reader.Close();
				reader.Dispose();
				comm.Dispose();
			}
			catch (Exception ex)
			{
			}
		}

		private void QLROLE_XOA_ItemClick(object sender, ItemClickEventArgs e)
		{
			string rolename = "";
			if (panel_QLROLE_list_curr_user.Focused == true && panel_QLROLE_list_curr_user.SelectedItems.Count > 0)
				rolename = panel_QLROLE_list_curr_user.SelectedItems[0].Text;
			else if (panel_QLROLE_list.Focused == true && panel_QLROLE_list.SelectedItems.Count > 0)
				rolename = panel_QLROLE_list.SelectedItems[0].Text;
			if (rolename != "")
				if (MessageBox.Show("Bạn chắc chắn muốn xóa role \"" + rolename + "\" ?", "Warn", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
				{
					OracleCommand comm = new OracleCommand();
					comm.Connection = orcl.conn;
					comm.CommandText = "drop role \"" + rolename + "\"";
					try
					{
						comm.ExecuteNonQuery();
						comm.Dispose();
						MessageBox.Show("Xóa role \"" + rolename + "\" thành công", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);
						panel_QLROLE_list.Items.Remove(panel_QLROLE_list.SelectedItems[0]);
					}
					catch (Exception ex)
					{
						MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
					}
				}
		}

		private void QLROLE_THEM_ItemClick(object sender, ItemClickEventArgs e)
		{
			Form_Role form_role = new Form_Role(1, null);
			form_role.ShowDialog();
		}

		private void QLROLE_USERS_ItemClick(object sender, ItemClickEventArgs e)
		{
			panel_QLROLE_users.Items.Clear();
			string rolename = "";
			if (panel_QLROLE_list_curr_user.Focused == true && panel_QLROLE_list_curr_user.SelectedItems.Count > 0)
				rolename = panel_QLROLE_list_curr_user.SelectedItems[0].Text;
			else if (panel_QLROLE_list.Focused == true && panel_QLROLE_list.SelectedItems.Count > 0)
				rolename = panel_QLROLE_list.SelectedItems[0].Text;
			if (rolename != "")
			{
				OracleCommand comm = new OracleCommand();
				comm.Connection = orcl.conn;
				comm.CommandText = "select * from dba_role_privs where GRANTED_ROLE = '" + rolename + "'";
				try
				{
					OracleDataReader reader = comm.ExecuteReader();
					while (reader.Read())
					{
						ListViewItem lvi = panel_QLROLE_users.Items.Add(reader["grantee"].ToString());
						lvi.SubItems.Add(reader["admin_option"].ToString());
					}
					reader.Close();
					reader.Dispose();
					comm.Dispose();
				}
				catch (Exception ex)
				{
					MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}

		private void QLROLE_SUA_ItemClick(object sender, ItemClickEventArgs e)
		{
			string rolename = "";
			if (panel_QLROLE_list_curr_user.Focused == true && panel_QLROLE_list_curr_user.SelectedItems.Count > 0)
				rolename = panel_QLROLE_list_curr_user.SelectedItems[0].Text;
			else if (panel_QLROLE_list.Focused == true && panel_QLROLE_list.SelectedItems.Count > 0)
				rolename = panel_QLROLE_list.SelectedItems[0].Text;
			if (rolename != "")
			{
				Form_Role form_role = new Form_Role(2, rolename);
				form_role.ShowDialog();
			}
		}

		private void QLROLE_SYSPRIVS_ItemClick(object sender, ItemClickEventArgs e)
		{
			string role_name = "";
			if (panel_QLROLE_list_curr_user.Focused == true && panel_QLROLE_list_curr_user.SelectedItems.Count > 0)
				role_name = panel_QLROLE_list_curr_user.SelectedItems[0].Text;
			else if (panel_QLROLE_list.Focused == true && panel_QLROLE_list.SelectedItems.Count > 0)
				role_name = panel_QLROLE_list.SelectedItems[0].Text;
			if (role_name != "")
			{
				Form_Role_SysPrivs form_role_sysprivs = new Form_Role_SysPrivs(1, role_name);
				form_role_sysprivs.ShowDialog();
			}
		}

		private void QLROLE_GRANTEDROLES_ItemClick(object sender, ItemClickEventArgs e)
		{
			string role_name = "";
			if (panel_QLROLE_list_curr_user.Focused == true && panel_QLROLE_list_curr_user.SelectedItems.Count > 0)
				role_name = panel_QLROLE_list_curr_user.SelectedItems[0].Text;
			else if (panel_QLROLE_list.Focused == true && panel_QLROLE_list.SelectedItems.Count > 0)
				role_name = panel_QLROLE_list.SelectedItems[0].Text;
			if (role_name != "")
			{
				Form_Role_GrantedRole form_role_grantedrole = new Form_Role_GrantedRole(1, role_name);
				form_role_grantedrole.ShowDialog();
			}
		}

		

    }
}